﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Collections.Generic;

namespace wzSqlAdmin.Util
{
    public class Impostazioni
    {
        private static IsolatedStorageSettings userSettings = IsolatedStorageSettings.ApplicationSettings;
        public string Type { get; set; }
        public string Server { get; set; }
        public string UID { get; set; }
        public string PWD { get; set; }
        public bool trusted { get; set; }
        public int port { get; set; }
        public override string ToString()
        {
            return this.Server;

        }
        public void setImpostazione(string Type, string Server, string UID, string PWD, bool trusted, int port)
        {
            int saved = 1;
            if (string.IsNullOrEmpty(UID))
            {
                UID = "";
            }
            if (string.IsNullOrEmpty(PWD))
            {
                PWD = "";
            }
            Impostazioni i = GetImpostazione(Server);
            if (i == null)
            {
                try
                {
                    saved = (int)userSettings["saved"];
                    saved += 1;
                    userSettings["saved"] = saved;
                    userSettings["type" + saved] = Type;
                    userSettings["server" + saved] = Server;
                    userSettings["UID" + saved] = UID;
                    userSettings["PWD" + saved] = PWD;
                    userSettings["trusted" + saved] = trusted;
                    userSettings["port" + saved] = port;

                }
                catch (System.Collections.Generic.KeyNotFoundException)
                {
                    // No preference is saved.
                    userSettings.Add("saved", saved);
                    userSettings.Add("type" + saved, Type);
                    userSettings.Add("server" + saved, Server);
                    userSettings.Add("UID" + saved, UID);
                    userSettings.Add("PWD" + saved, PWD);
                    userSettings.Add("trusted" + saved, trusted);
                    userSettings.Add("port" + saved, port);
                }
            }
            else
            {
                userSettings["type" + _lastread] = Type;
                userSettings["server" + _lastread] = Server;
                userSettings["UID" + _lastread] = UID;
                userSettings["PWD" + _lastread] = PWD;
                userSettings["trusted" + _lastread] = trusted;
                userSettings["port" + _lastread] = port;
            }
        }
        public List<Impostazioni> GetImpostazioni(string Tipo)
        {
            int saved = 0;
            try
            {
                saved = (int)userSettings["saved"];
            }
            catch (System.Collections.Generic.KeyNotFoundException)
            {
                // No preference is saved.
            }
            List<Impostazioni> choices = new List<Impostazioni>();
            string iType = "";
            string iServer = "";
            string iUID = "";
            string iPWD = "";
            bool itrusted = false;
            int iport = 0;
            for (int i = 1; i <= saved; i++)
            {
                try
                {
                    iType = (string)userSettings["type" + i];
                    if (iType == Tipo)
                    {
                        iServer = (string)userSettings["server" + i];
                        iUID = (string)userSettings["UID" + i];
                        iPWD = (string)userSettings["PWD" + i];
                        itrusted = (bool)userSettings["trusted" + i];
                        iport = (int)userSettings["port" + i];
                        choices.Add(new Impostazioni()
                        {
                            Server = iServer,
                            UID = iUID,
                            PWD = iPWD,
                            trusted = itrusted,
                            port = iport,
                            Type = iType
                        }
                            );
                    }
                }
                catch (System.Collections.Generic.KeyNotFoundException)
                {
                    // No preference is saved.
                }

            }
            return choices;
        }
        private int _lastread = 0;
        public Impostazioni GetImpostazione(string Server)
        {
            int saved = 0;
            try
            {
                saved = (int)userSettings["saved"];
            }
            catch (System.Collections.Generic.KeyNotFoundException)
            {
                // No preference is saved.
            }
            Impostazioni Srv = new Impostazioni();
            for (int i = 1; i <= saved; i++)
            {
                try
                {
                    if (userSettings["server" + i].ToString().ToLower() == Server.ToLower())
                    {
                        Srv.Type = (string)userSettings["type" + i];
                        Srv.Server = (string)userSettings["server" + i];
                        Srv.UID = (string)userSettings["UID" + i];
                        Srv.PWD = (string)userSettings["PWD" + i];
                        Srv.trusted = (bool)userSettings["trusted" + i];
                        Srv.port = (int)userSettings["port" + i];
                        _lastread = i;
                    }
                }
                catch (System.Collections.Generic.KeyNotFoundException)
                {
                    // No preference is saved.
                }

            }
            return Srv;
        }

    }
}
